package com.guigu.www.denghao.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.guigu.www.denghao.service.DHQuestionService;
import com.guigu.www.mapper.QuestionDetailsMapper;
import com.guigu.www.mapper.QuestionMapper;
import com.guigu.www.mapper.RubricMapper;
import com.guigu.www.pojo.PageVo;
import com.guigu.www.pojo.Question;
import com.guigu.www.pojo.QuestionDetails;
import com.guigu.www.pojo.Rubric;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DHQuestionServiceImpl implements DHQuestionService {

    @Autowired
    private QuestionMapper questionMapper;

    @Autowired
    private QuestionDetailsMapper detailsMapper;

    @Autowired
    private RubricMapper rubricMapper;

    @Override
    public PageVo<Question> initQues(Integer pageNum, Integer pageSize, String quesId, String quesName, String quesType) {
        IPage<Question> iPage = new Page<>(pageNum,pageSize);
        QueryWrapper<Question> wrapper = new QueryWrapper<>();
        wrapper.like("ques_name",quesName);
        wrapper.ne("ques_state",2);
        if (!quesId.equals("")){
            wrapper.eq("ques_id",quesId);
        }
        if (!quesType.equals("")){
            wrapper.eq("ques_type",quesType);
        }
        iPage = questionMapper.selectPage(iPage,wrapper);
        return new PageVo<>(iPage.getTotal(),iPage.getRecords());
    }

    @Override
    public void addQues(Question question, Integer[] questionDetails) {
        questionMapper.insert(question);
        for (Integer questionDetail : questionDetails) {
            QuestionDetails  details = new QuestionDetails(null,question.getQuesId(),questionDetail);
            detailsMapper.insert(details);
        }
    }

    @Override
    public List<Rubric> watchQues(Integer quesId) {
        return rubricMapper.queryRubricByQuesId(quesId);
    }

    @Override
    public List<Rubric> findByTestType(Integer quesTestType) {
        return rubricMapper.selectList(new QueryWrapper<Rubric>().eq("rub_type",quesTestType));
    }

    @Override
    public List<Question> clickRubType(Integer quesTestType) {
        return questionMapper.selectList(new QueryWrapper<Question>().eq("ques_test_type",quesTestType).ne("ques_state",2));
    }

    @Override
    public Integer updateQuesId(Integer quesId, Integer quesState) {
        Question question = new Question(quesId,null,null,null,null,quesState);
        return questionMapper.updateById(question);
    }
}
